package com.ruoyi.applet.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.ruoyi.applet.domain.GoodsDoc;
import com.ruoyi.applet.domain.Images;
import com.ruoyi.applet.domain.Specs;
import com.ruoyi.applet.service.*;
import com.ruoyi.applet.utli.TokenByUsrId;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.applet.domain.Goods;
import com.ruoyi.common.core.web.controller.BaseController;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/applet")
public class GoodsController extends BaseController
{
    @Resource
    private IGoodsService goodsService;

    @Resource
    private ISpecsService specsService;

    @Resource
    private ISpecValuesService specValuesService;

    @RequestMapping("/goodsList")
    public Object selectGoodsList(@RequestParam("token") String token){
        return goodsService.selectAllByGoodsImgIdList(TokenByUsrId.tokenByUsrId(token));
    }

    @RequestMapping("/goodsImageList")
    public Object selectGoodsImageList(@RequestParam("goodsId") Long goodsId){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("imageList",goodsService.selectAllByGoodsIdList(goodsId));
        resultMap.put("goods",goodsService.selectAllByGoodsId(goodsId));
        return resultMap;
    }

    @RequestMapping("/specifications")
    public Object selectGoodsSpecifications(@RequestParam("goodsId") Long goodsId){
        Map<String,Object> resultMap = new HashMap<>();
        List<Specs> specsList = specsService.selectBySpecsNameList(goodsId);
        resultMap.put("specs",specsList);
        resultMap.put("specsValues",specValuesService.selectBySpecValuesList(goodsId));
        return resultMap;
    }

    @RequestMapping("/goodsSpecifications")
    public Object selectSpecifications(
                                       @RequestParam("specValue1") String specValue1 ,
                                       @RequestParam("specValue2") String specValue2 ,
                                       @RequestParam("specValue3") String specValue3 ,
                                       @RequestParam("goodsId") Long goodsId){
        return goodsService.selectAllByGoodsIdSpecifications(specValue1, specValue2, specValue3, goodsId);
    }

    @RequestMapping("/goodsOrder")
    public Object selectGoodsOrder(@RequestParam("productSpecId") Long productSpecId){
        return goodsService.selectAllByProductSpecId(productSpecId);
    }

    @Resource
    ElasticSearchRepository elasticSearchRepository;

    @Resource
    ElasticSearchService elasticSearchService;

    @Resource
    IImagesService iImagesService;

    @RequestMapping( value = "/goodsNameList",produces = "application/json;charset=UTF-8")
    public Object getList(@RequestParam("categoryName") String goodsName) {
        addElastics();
        List<Images> imagesList = new ArrayList<>();
        List<SearchHit<GoodsDoc>> hitList = elasticSearchRepository.findAllByGoodsName(goodsName);
        for (SearchHit<GoodsDoc> goodsDocSearchHit : hitList) {
            imagesList.add(iImagesService.setImagesIdImagesList(goodsDocSearchHit.getContent().getGoodsImgId()));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("imagesList",imagesList);
        resultMap.put("hitList",hitList);
        return resultMap;
    }

    public void addElastics(){
        List<Goods> list = elasticSearchService.ElasticGood();
        List<GoodsDoc> goodsDocList = new ArrayList<>();
        for (Goods goods : list) {
            GoodsDoc goodsDoc = new GoodsDoc(goods);
            goodsDocList.add(goodsDoc);
        }
        elasticSearchRepository.saveAll(goodsDocList);
    }
}
